package com.huang.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import java.util.HashSet;
import java.util.Set;

public class MD5Realm extends AuthorizingRealm {
    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

        String name = (String) principalCollection.getPrimaryPrincipal();
        Set<String> set = new HashSet<>();
        set.add("admin");
        if("xiaohuang".equals(name)){
            SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo(set);
           // authorizationInfo.addRole("user");  根据角色赋予权限
            authorizationInfo.addStringPermission("user:*:01");  //  基于字符串访问控制 根据数据库查询权限信息  赋值给权限对象
            return authorizationInfo;
        }

        //根据name查询数据库信息  拿到具有什么权限  传入给AuthorizationInfo 进行返回

        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String principal = (String) authenticationToken.getPrincipal();
        //查询数据库
        if(principal.equals("xiaohuang")){
          //  return new SimpleAuthenticationInfo(principal,"202cb962ac59075b964b07152d234b70",this.getName());
            return new SimpleAuthenticationInfo(principal,"083bc5f906aba3d7144dc44c2f32bf89"
                    , ByteSource.Util.bytes("xo*7px"),this.getName());  //使用随机盐
        }
        return null;
    }
}
